Reads the <title> tag of a web page.

Instructions: Don't forget we can't parse it as an XML document due to old HTML standards, and simple mistakes.

==========================================================

/*
 * Get the title element from a URL
 * Author: Danny Battison
 * Contact: gabehabe@gmail.com
 */

function getTitle($url) {
	// we can't treat it as an XML document because some sites aren't valid XHTML
	// so, we have to use the classic file reading functions and parse the page manually
	$fh = fopen($url, "r");
	$str = fread($fh, 7500);  // read the first 7500 characters, it's gonna be near the top
	fclose($fh);
	$str2 = strtolower($str);
	$start = strpos($str2, "<title>")+7;
	$len   = strpos($str2, "</title>") - $start;
	return substr($str, $start, $len);
}
